from django.db import models
from utils.utils import get_host_ip
from frame_server.model import BaseModel

# 错误日志
class LogError(BaseModel):
    userId = models.ForeignKey(to="user.User", related_name="log_error", db_column="user_id", db_constraint=False, on_delete=models.CASCADE, null=True,blank=True, verbose_name="用户id")
    env = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name="系统环境")
    serverIp = models.CharField(max_length=255, db_column="server_ip", default=get_host_ip, null=True, blank=True, verbose_name="服务ip")
    visitorIp = models.CharField(max_length=255, db_column="visitor_ip", null=True, blank=True, verbose_name="访问者ip")
    method = models.CharField(max_length=10, null=True, blank=True, verbose_name="请求方式")
    url = models.CharField(max_length=255, null=True, blank=True, verbose_name="请求地址")
    userAgent = models.CharField(max_length=1000, db_column="user_agent", null=True, blank=True, verbose_name="用户代理")
    message = models.TextField(null=True, blank=True, verbose_name="错误信息")
    exceptionClass = models.CharField(max_length=255,db_column="exception_class", null=True, blank=True, verbose_name="异常类")
    data = models.TextField(null=True, blank=True, verbose_name="提交的数据")
    createTime = models.DateTimeField(db_column="create_time",auto_now_add=True, null=True, blank=True, verbose_name="创建时间")

    class Meta:
        db_table = 'system_log_error'
        ordering = ['-createTime']
        indexes = [
            models.Index(fields=['url']),
            models.Index(fields=['createTime']),
        ]

